﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;

namespace _3D_FileExplorer
{
    public class LeafPrimitive : Primitives
    {

        public Matrix branchTranslation = Matrix.Identity;
        public string path;
        public bool picked = false;
        public bool opened = false;
        Texture2D texture;
        Texture2D textBack = Fe.content.Load<Texture2D>(@"Textures\textback");
        SpriteFont font = Fe.content.Load<SpriteFont>(@"Fonts\Arial");
        private bool picture;


        public LeafPrimitive(Vector3 center, string path, String shortName, string type,long id,float weight, Matrix translation) : base(Matrix.Identity,Matrix.Identity,Color.White,"",shortName,type,id,weight)
        {
            this.path = path;
            this.center = center;
            this.branchTranslation = translation; 
            GetTexture();
            CreateVertices(type);
            CreateIndices();
            CreateBoudingSphere();
            LoadBuffer();
        }

        private void CreateVertices(string type)
        {
            float textureY = 1f / 4f;
            float textureX = 1f / 2f;

            float indexX, indexY;


            indexX = Fe.td.GetX(type);
            indexY = Fe.td.GetY(type);

            if (picture == false)
            {
                AddVertex(new Vector3(-1, 0, -1), new Vector2(textureX * (indexX - 1), textureY * (indexY - 1)));
                AddVertex(new Vector3(1, 0, -1), new Vector2(textureX * indexX, textureY * (indexY - 1)));
                AddVertex(new Vector3(1, 0, 1), new Vector2(textureX * indexX, textureY * indexY));
                AddVertex(new Vector3(-1, 0, 1), new Vector2(textureX * (indexX - 1), textureY * indexY));
            }
            else
            {
                AddVertex(new Vector3(-1, 0, -1), new Vector2(0, 0));
                AddVertex(new Vector3(1, 0, -1), new Vector2(1, 0));
                AddVertex(new Vector3(1, 0, 1), new Vector2(1, 1));
                AddVertex(new Vector3(-1, 0, 1), new Vector2(0, 1));
            }
        }

        private void CreateIndices()
        {
            indices.Add(0);
            indices.Add(1);
            indices.Add(2);
            indices.Add(0);
            indices.Add(2);
            indices.Add(3);
        }



        public void AddVertex(Vector3 vertice, Vector2 coord)
        {
            vertices.Add(new VertexPositionColorTexture(vertice, Color.White, coord));
            pvertices.Add(vertice);
        }


        public void SetTransform(Matrix rotation, Matrix translation)
        {
            fulltransform = rotation * translation* branchTranslation;
        }

        private void GetTexture()
        {
            //files
            try
            {
                texture = Texture2D.FromFile(Fe.graphics.GraphicsDevice, path, 80, 80);
                picture = true;
            }
            catch (Exception e)
            {
                picture = false;
                texture = Fe.content.Load<Texture2D>(@"Textures/textura");

            }
        }

        public void SetPickedTexture()
        {
           
        }


        public override void Draw(BasicEffect effect, Camera camera, GraphicsDevice Device)
        {
            effect.World = fulltransform;
            effect.View = camera.view;
            effect.Projection = camera.projection;
            effect.VertexColorEnabled = true;
            effect.TextureEnabled = true;
            effect.Texture = texture;
            effect.Alpha = 0.6f;
            if (picked == true)
            {
                effect.Alpha = 1f;
            }
            effect.Begin();

            Device.RenderState.AlphaBlendEnable = true;
            Device.RenderState.SourceBlend = Blend.SourceAlpha;

            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Begin();
                //Tell the GPU what vertex array to pull vertex data from.
                Device.Indices = indexBuffer;
                Device.Vertices[0].SetSource(vertexBuffer, 0, VertexPositionColorTexture.SizeInBytes);

                int primitiveCount = indices.Count / 3;

                Device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, vertices.Count, 0, primitiveCount);
                pass.End();
            }

            effect.End();

            Device.RenderState.AlphaBlendEnable = false;


            #region DRAW_TEXT_LABELS

            Vector3 screenSpace;
            Vector2 textPosition;


            screenSpace = Device.Viewport.Project(pvertices[3], camera.projection, camera.view, fulltransform);
            float distance = Vector3.Distance(camera.cameraPosition,Vector3.Transform(boudingSphere.Center,fulltransform));
            if (distance < 28f)
            {
                textPosition.X = screenSpace.X;
                textPosition.Y = screenSpace.Y + 5;
                float windowResizeScale = (float)Fe.form.Height/(float)Fe.form.MinimumSize.Height;
                Console.WriteLine(windowResizeScale);
                Fe.spriteBatch.Begin();

                Fe.spriteBatch.Draw(textBack, textPosition, null, Color.Black, 0, Vector2.Zero, new Vector2(0.16f*windowResizeScale, 0.15f*windowResizeScale), SpriteEffects.None, 1);
                textPosition.Y += 2.5f;
                textPosition.X += 3;
                Fe.spriteBatch.DrawString(font, shortName, textPosition, Color.White, 0, Vector2.Zero, 0.15f*windowResizeScale, SpriteEffects.None, 0);
                Fe.spriteBatch.End();
            }
            #endregion

        }

    }
}
